package response

import (
	"gin-proxy/common/errdef"
	"net/http"

	"github.com/gin-gonic/gin"
)

const (
	SUCCESS = iota
)

type Response struct {
	Code int32  `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

func result(c *gin.Context, code int32, msg string, data any) {
	resp := &Response{
		Code: code,
		Msg:  msg,
		Data: data,
	}

	c.JSON(http.StatusOK, resp)
}

func Ok(c *gin.Context, data any) {
	result(c, SUCCESS, "", data)
}

func Fail(c *gin.Context, err errdef.BizErr) {
	result(c, err.Code(), err.Msg(), nil)
}

// FailD fail with data
func FailD(c *gin.Context, err errdef.BizErr, data any) {
	result(c, err.Code(), err.Msg(), data)
}
